package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

@RestController("adminShopController")
@Slf4j
@RequestMapping("/admin/shop")
@Api(tags = "店铺相关接口")
public class ShopController {
    // 营业状态数据存储方式：基于Redis的字符串来进行存储
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String SHOP_STATUS = "SHOP_STATUS";

    @GetMapping("/status")
    @ApiOperation("获取当前营业状态")
    public Result<Integer> getShopStatus() {
        // 从Redis中获取当前营业状态
        Integer status = (Integer) redisTemplate.opsForValue().get(SHOP_STATUS);
        log.info("更新当前营业状态：{}", (status == 1 ? "营业中" : "休息中"));
        return Result.success(status);
    }

    @PutMapping("/{status}")
    @ApiOperation("更新当前营业状态")
    public Result<String> updateShopStatus(@PathVariable("status") Integer status) {
        log.info("更新当前营业状态：{}", (status == 1 ? "营业中" : "休息中"));
        // 更新当前营业状态到Redis
        redisTemplate.opsForValue().set(SHOP_STATUS, status);
        return Result.success();
    }
}
